package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingPlanServiceImpl implements NursingPlanService {
    final NursingPlanMapper nursingPlanMapper;
    final NursingProjectPlanMapper nursingProjectPlanMapper;
    final NursingLevelMapper nursingLevelMapper;

    //分页查询护理计划
    @Override
    public PageBean<NursingPlanVo> pageQuery(NursingPlanPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingPlanVo> page = nursingPlanMapper.pageQuery(dto);
        return PageBean.of(page, NursingPlanVo.class);
    }


    //添加护理计划
    @Override
    @Transactional
    public void insertPlan(NursingPlanDto dto) {
        //1. 添加一条护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);

      /*  nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());

        nursingPlan.setCreateBy(1671403256519078138L);
        nursingPlan.setUpdateBy(1671403256519078138L);*/

        nursingPlan.setStatus(1); //0禁用、1启用

        nursingPlanMapper.insert(nursingPlan); //注意：设置主键自增返回


        //2. 添加多条护理计划护理项目关系
        List<NursingProjectPlanDto> dtoList = dto.getProjectPlans();
        List<NursingProjectPlan> poList = BeanUtil.copyToList(dtoList, NursingProjectPlan.class);

        poList.forEach(projectPlan -> {
            //必须要设置projectPlan的plan_id
            projectPlan.setPlanId(nursingPlan.getId());
            //添加操作
            nursingProjectPlanMapper.insert(projectPlan);
        });
    }

    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        List<NursingProjectPlanVo> projectPlanVos = nursingProjectPlanMapper.getByPlanId(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 根据id修改护理计划
     *
     * @param dto
     */
    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto dto) {
        //1. 根据id修改护理计划基本信息
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
     /*   nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1671403256519078138L);*/
        nursingPlanMapper.updateById(nursingPlan);

        //2. 根据id修改护理计划对应的护理项目关系
        //2.1. 根据id删除对应的所有护理项目关系
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
        //2.2. 重新添加关系
        List<NursingProjectPlanDto> dtoList = dto.getProjectPlans();
        List<NursingProjectPlan> poList = BeanUtil.copyToList(dtoList, NursingProjectPlan.class);

        poList.forEach(projectPlan -> {
            //必须要设置projectPlan的plan_id
            projectPlan.setPlanId(nursingPlan.getId());
            //添加操作
            nursingProjectPlanMapper.insert(projectPlan);
        });
    }

    /**
     * 根据id删除
     *
     *
     */
    @Override
    public void deletePlanById(Long id) {
            NursingPlan byId = nursingPlanMapper.getById(id);
            if (ObjectUtil.equal(byId.getStatus(), 1)){
                //启用状态不能删除
                throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
            }

            List<NursingLevel> list = nursingLevelMapper.getByPlanId(id);
            if(CollUtil.isNotEmpty(list)) {
                //不为空，说明该护理计划被护理等级关联，不能删除
                throw new BaseException(BasicEnum.DELETE_PROJECT_FAIL);
            }

            //删除护理计划
            nursingPlanMapper.deleteById(id);
            //删除护理计划对应的护理等级关系
            nursingProjectPlanMapper.deleteByPlanId(id);
    }

    /**
     * 启用或禁用
     *
     *
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingPlan nursingPlan = NursingPlan.builder().id(id).status(status).build();
        nursingPlanMapper.updateById(nursingPlan);
    }

    /**
     * 查询所有
     *
     *
     */
    @Override
    public List<NursingPlanVo> findAll() {
        return nursingPlanMapper.findAll();
    }


}
